﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using Microsoft.SharePoint.Taxonomy;

namespace MS.InformationManagement.BLL.GlobalCommands
{
    public class ChangeStateCommand : IBaseCommand
    {
        #region Fields

        Office.DocumentSet _currentDocumentSet;
        TaxonomyFieldValue _currentState;
        string _newState;

        #endregion

        #region Constructors

        public ChangeStateCommand(Office.DocumentSet currentDocumentSet, string newState)
        {
            _currentDocumentSet = currentDocumentSet;
            _newState = newState;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            _currentState = (TaxonomyFieldValue)_currentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS];
            _currentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_STATUS, _newState);
            _currentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_CAPTURED] = true;            
            _currentDocumentSet.Folder.Item.Update();
            _currentDocumentSet.Folder.Update();
        }

        public void Undo()
        {
            _currentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = _currentState;
            _currentDocumentSet.Folder.Item.Update();
            _currentDocumentSet.Folder.Update();
        }

        #endregion
    }
}
